<?php

class Core_Model_Article_Category extends Dcmp_Model_Entity {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_article_category');
    }

    protected function beforeInsert($transaction) {
        $parent = new self();
        if ($this->getParentId() && $this->getParentId() != -1) {
            $idPath = $parent->load($this->getParentId())->getIdPath();
            $this->setIdPath($idPath);
        } else {
            $this->setParentId(0);
            $this->setIdPath('');
        }
        $this->setLevel($parent->getLevel() + 1);
        $sorting = $this->getTable()->getMaxSorting($this->getParentId()) + 1;
        $this->setSorting($sorting);
        parent::beforeInsert($transaction);
    }

    protected function afterInsert($transaction) {
        $this->getTable()->insertIdPath($this->getId(), $this->getIdPath() . $this->getId() . '_', $transaction);
        if($this->issetData('sef_url')){
            App::getHelper('global/sef')->addSefUrl('article_category', $this->getId(), '/article/category/id/' . $this->getId(), $this->getSefUrl());
        }

        parent::afterInsert($transaction);
    }

    protected function beforeUpdate($transaction) {
        if ($this->propertyIsChanged('parent_id')) {
            $sorting = $this->getTable()->getMaxSorting($this->getParentId()) + 1;
            $this->setSorting($sorting);
            $parent = new self();
            if ($this->getParentId() && $this->getParentId() != -1) {
                $idPath = $parent->load($this->getParentId())->getIdPath() . $this->getId() . '_';
                $this->setIdPath($idPath);
            } else {
                $this->setParentId(0);
                $this->setIdPath($this->getId() . '_');
            }
            $this->setLevel($parent->getLevel() + 1);
        }
        parent::beforeUpdate($transaction);
    }

    protected function afterUpdate($transaction) {
        if ($this->propertyIsChanged('sef_url')) {
            App::getHelper('global/sef')->modifySef('article_category', $this->getId(), '/article/category/id/' . $this->getId(), $this->getSefUrl());
        }
        if ($this->propertyIsChanged('parent_id')) {
            $this->getTable()->updateIdPath($this->getOriginalData('id_path'), $this->getIdPath(), $transaction);
        }
        parent::afterUpdate($transaction);
    }

    protected function initializeTable($table) {
        $table->setName('{t}article_category');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('title', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('theme_id', 'guid');
        $table->createColumn('parent_id', 'integer')->setDefaultValue(0);
        $table->createColumn('id_path', 'string');
        $table->createColumn('sorting')->setUpdateable(FALSE);
        $table->createColumn('level')->setDefaultValue(1);
        $table->createColumn('area_id', 'integer')->setUpdateable(FALSE);
        $table->createColumn('menu_id', 'integer');
        $table->createColumn('creator_id', 'integer');
        $table->createColumn('created_time', 'datetime')->setDefaultValue(date('Y-m-d H:i:s'));
    }

    public function getChildren() {
        return App::getModel('article/collection_category')
                ->like('id_path', ':idPath:s')
                ->addParameter('idPath', $this->getIdPath().'%')
                ->load();
    }
}